package com.baosight.risk.service.riaa.impl;

import com.baosight.risk.api.riaa.RiquInquiryCompExtService;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.fltd.FltdProcessMapper;
import com.baosight.risk.mapper.fltd.FltdTaskMapper;
import com.baosight.risk.mapper.riaa.RiquInquiryCompMapper;
import com.baosight.risk.mapper.riaa.RiquInquiryDetailMapper;
import com.baosight.risk.service.fltd.entity.FltdTask;
import com.baosight.risk.service.riaa.entity.RiquInquiryComp;
import com.baosight.risk.service.riaa.entity.RiquInquiryDetail;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author RayWade
 * @create 2019-12-27 14:07
 */
@Service
public class RiquInquiryCompExtServiceImpl implements RiquInquiryCompExtService {

    @Autowired
    private RiquInquiryCompMapper inquiryCompMapper;

    @Autowired
    private RiquInquiryDetailMapper inquiryDetailMapper;

    @Autowired
    private FltdTaskMapper fltdTaskMapper;

    @Autowired
    private FltdProcessMapper fltdProcessMapper;
    /**
     * 根据id查询出本节点及其下属节点
     *
     * @param id
     */
    @Override
    public List<RiquInquiryComp> inquiryCompList(String id) {
        List<RiquInquiryComp> compList = Lists.newArrayList();
        RiquInquiryComp inquiryComp = inquiryCompMapper.selectByPrimaryKey(id);
        List<RiquInquiryComp> list = inquiryCompMapper.queryByParentGuid(id, null);
        compList.add(inquiryComp);
        compList.addAll(list);
        return compList;
    }

    /**
     * 问卷调研结果新增
     *
     * @param list
     */
    @Override
    public void saveInquiryCompExt(List<RiquInquiryComp> list) throws Exception {
        for (RiquInquiryComp inquiryComp : list) {
            insertData(inquiryComp);
        }
    }

    private void insertData(RiquInquiryComp inquiryComp) throws Exception {
        // 检验数据是否已经有过下发
        RiquInquiryComp comp = new RiquInquiryComp();
        comp.setSubmitCompCode(inquiryComp.getSubmitCompCode());
        comp.setSubmitUserCode(inquiryComp.getSubmitUserCode());
        comp.setQuId(inquiryComp.getQuId());
        List<RiquInquiryComp> list = inquiryCompMapper.selectList(comp);
        if (CollectionUtils.isNotEmpty(list)) {
            throw new Exception(inquiryComp.getSubmitUserName() + "在" + inquiryComp.getSubmitCompName() + "已存在数据!");
        }
        if (StringUtils.isEmpty(inquiryComp.getId())) {
            inquiryComp = (RiquInquiryComp)BeanUtils.initBean(inquiryComp);
            inquiryCompMapper.insert(inquiryComp);
        } else {
            inquiryComp = (RiquInquiryComp) BeanUtils.updateBean(inquiryComp);
            inquiryCompMapper.updateByPrimaryKeySelective(inquiryComp);
        }
    }

    /**
     * 问卷调研删除
     *
     * @param ids
     * @return
     */
    @Override
    public void deleteInquiryCompExt(List<String> ids) {
        ids.forEach(s -> deleteInquiryComp(s));

    }

    private void deleteInquiryComp(String id){
        RiquInquiryComp comp = inquiryCompMapper.selectByPrimaryKey(id);
        if ("A01".equals(comp.getStatus())) {
            inquiryCompMapper.deleteByPrimaryKey(id);
        } else if ("A02".equals(comp.getStatus())){
            List<RiquInquiryDetail> list = inquiryDetailMapper.queryByResultId(id);
            List<String> ids = list.stream().map(RiquInquiryDetail::getId).collect(Collectors.toList());
            inquiryDetailMapper.deleteByIds(ids);
            // 删除待办
            deleteTask(id);

        }
    }

    private void deleteTask(String id) {
        List<FltdTask> taskList = fltdTaskMapper.queryByGuid(id);
        taskList.forEach(o -> {
            fltdTaskMapper.deleteByPrimaryKey(o.getId);
            fltdProcessMapper.deleteByPrimaryKey(o.getProcGuid());
        });
    }
}
